package com.ybkj.o2o.zhuanche.amqp;

import com.ybkj.o2o.zhuanche.amqp.docking.*;
import com.ybkj.o2o.zhuanche.amqp.rabbitTemplate.PushTemplate;
import com.ybkj.o2o.zhuanche.common.api.DaijiaApi;
import com.ybkj.o2o.zhuanche.entity.Order;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by Administrator on 2017/2/8.
 */
@Component
public class DockingSuperviseApi {
    @Autowired
    @Qualifier("dsRabbitTemplate")
    private RabbitTemplate dsRabbitTemplate;

    @Autowired
    private PushTemplate pushTemplate;

    @Autowired
    private DaijiaApi daijiaApi;


//  private static final String daijiaURL = "123.57.53.37:8080";//本地代驾地址

    public void pushDdjsMessage(Order order, String passengerPhone, int passengerGender,
                                Date serverDate, String startAddress, Double startLat, Double startLng,
                                String serviceType, String endAddress, Double endLat, Double endLng, long areaId, String areaName, String memo) {
        DdjsMessage ddjsMessage = new DdjsMessage();
        ddjsMessage.setOrderNo(order.getOrderNo());
        ddjsMessage.setPassengerName(order.getPassengerName());
        ddjsMessage.setPassengerGender(passengerGender == 0 ? "MALE" : "FEMALE");
        ddjsMessage.setPassengerTel(passengerPhone);
        ddjsMessage.setUseLocale(startAddress);
        ddjsMessage.setUseLon(startLng);
        ddjsMessage.setUseLat(startLat);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ddjsMessage.setUseTime(formatter.format(serverDate));
        ddjsMessage.setOrderTime(formatter.format(new Date()));
        ddjsMessage.setDestination(endAddress);
        //下单时，起始点和终点距离太大，不能获取终点数据
        if (!String.valueOf(endLat).equals("null") && !String.valueOf(endLng).equals("null")) {
            ddjsMessage.setDestLat(endLat);
            ddjsMessage.setDestLon(endLng);
        }
        ddjsMessage.setPassengerDemand(memo);
        ddjsMessage.setIsVoice("N");
        ddjsMessage.setVoiceUrl("");
        ddjsMessage.setPassengerType("GP");
        ddjsMessage.setIdentityNumber("");
        if ("jieji".equals(serviceType) || "songji".equals(serviceType)
                || "jiezhan".equals(serviceType) || "songzhan".equals(serviceType)) {
            ddjsMessage.setBespeakType(DdjsMessage.BESPEAKTYPE_SHUTTLE);
        } else if ("rizu".equals(serviceType) || "banrizu".equals(serviceType)) {
            ddjsMessage.setBespeakType(DdjsMessage.BESPEAKTYPE_DAILY_RENT);
        } else if ("tangzu".equals(serviceType)) {
            ddjsMessage.setBespeakType(DdjsMessage.BESPEAKTYPE_IMMEDIATE);
        } else {
            ddjsMessage.setBespeakType(DdjsMessage.BESPEAKTYPE_BESPEAK);
        }
        ddjsMessage.setTransPriceType(areaName);
        ddjsMessage.setAppKey(order.getAppkey());
        try {
            dsRabbitTemplate.convertAndSend(ddjsMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void pushDdcxMessage(Order order, String operator, String cause) {
        DdcxMessage ddcxMessage = new DdcxMessage();
        ddcxMessage.setOrderNo(order.getOrderNo());
        ddcxMessage.setCanceler(operator);
        if (DdcxMessage.DDCX_PASSENGER.equalsIgnoreCase(operator)) {
            ddcxMessage.setCancelTypeCode("1");
        } else if (DdcxMessage.DDCX_DRIVER.equalsIgnoreCase(operator)) {
            ddcxMessage.setCancelTypeCode("2");
        } else if (DdcxMessage.DDCX_PLATFORM.equalsIgnoreCase(operator)) {
            ddcxMessage.setCancelTypeCode("3");
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(new Date());
        ddcxMessage.setCancelTime(dateString);
        ddcxMessage.setCancelReason(cause);
        ddcxMessage.setAppKey(order.getAppkey());
        try {
            dsRabbitTemplate.convertAndSend(ddcxMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void pushCkpjMEssage(String orderNo, Double score, String content, String appKey) {
        CkpjMessage ckpjMessage = new CkpjMessage();
        ckpjMessage.setOrderNo(orderNo);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(new Date());
        ckpjMessage.setEvaluateTime(dateString);
        ckpjMessage.setDriverEvaluate(score);
        ckpjMessage.setVehicleEvaluate(score);
        ckpjMessage.setServiceEvaluate(score);
        ckpjMessage.setCommentType(content);
        ckpjMessage.setAppKey(appKey);
        ckpjMessage.setPushType(CkpjMessage.PJ);
        try {
            pushTemplate.sendMessage(ckpjMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void pushDdbgMessage(Order order, String toPlace, double latitude, double longitude) {
        DdbgMessage ddbgMessage = new DdbgMessage();
        ddbgMessage.setAppKey(order.getAppkey());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String Created = formatter.format(order.getCreated());
        ddbgMessage.setOrderTime(Created);
        ddbgMessage.setOrderNo(order.getOrderNo());
        ddbgMessage.setUseLat(order.getStartLat());
        ddbgMessage.setUseLon(order.getStartLng());
        ddbgMessage.setUseLocale(order.getStartAddress());
        String dateString = formatter.format(order.getServerTime());
        ddbgMessage.setUseTime(dateString);
        ddbgMessage.setIsVoice("N");
        ddbgMessage.setVoiceUrl("");
        String changeTime = formatter.format(new Date());
        ddbgMessage.setChangeTime(changeTime);
        ddbgMessage.setDestLat(latitude);
        ddbgMessage.setDestLon(longitude);
        ddbgMessage.setDestination(toPlace);
        ddbgMessage.setPassengerDemand(order.getMemo());
        Long areaId = order.getAreaId();
//      String transPriceType = daijiaApi.findAreaName(daijiaURL, areaId);

//      ddbgMessage.setTransPriceType(transPriceType);
        ddbgMessage.setTransPriceType("标准收费");
        try {
            pushTemplate.sendMessage(ddbgMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void pushJszqMessage(Order order) {
        JszqMessage jszqMessage = new JszqMessage();
        jszqMessage.setAgency(order.getAfterPayTypeDetail());
        jszqMessage.setBankCyclePeriod(1);
        jszqMessage.setAgencyType(Order.NO_BANK);
        jszqMessage.setVaild(true);
        try {
            dsRabbitTemplate.convertAndSend(jszqMessage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void pushDdwyMessage(Order order, int driverbreaker, String cause) {
        DdwyMessage ddwy = new DdwyMessage();
        ddwy.setAppKey(order.getAppkey());
        switch (driverbreaker) {
            case DdwyMessage.DRIVERBREAKER:
                ddwy.setBreaker(DdwyMessage.DRIVERBREAKER);
                ddwy.setCancelTypeCode("5");
                break;
            case DdwyMessage.PASSENGERBREAKER:
                ddwy.setBreaker(DdwyMessage.PASSENGERBREAKER);
                ddwy.setCancelTypeCode("4");
                break;
        }
        ddwy.setOrderNo(order.getOrderNo());
        ddwy.setBreakReason(cause);
        ddwy.setCancelTime(new Date());
        pushTemplate.sendMessage(ddwy);
    }
}
